// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        content : cc.Node,
        changePageEvents: {
            "default": [],
            type: cc.Component.EventHandler,
            tooltip: "i18n:COMPONENT.button.click_events"
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        // this.scollToIndex(4)

        this.node.on(cc.Node.EventType.TOUCH_START,this.onTouch.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouch.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_END,this.onTouch.bind(this));
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.onTouch.bind(this));
        this._touch = null;
        this.currentIdx = -1;
        this.hasScroll = false;
        this.updateIdx();
    },

    onTouch(event){
        if(this._touch && this._touch != event.touch){
            return;
        }
        
        if(event.type == cc.Node.EventType.TOUCH_START){
            this._touch = event.touch;
            this.touchx = event.getLocationX();
            this.content.stopAllActions();
            this.hasScroll = null;
            return;
        }
        if(event.type == cc.Node.EventType.TOUCH_END || event.type == cc.Node.EventType.TOUCH_CANCEL){
            // cc.log('event.type:',event.type)
            if(this.hasScroll){
                this.updateIdx();
            } else {
                var dp = this.node.convertToNodeSpaceAR(event.getLocation())
                var len = this.content.children.length
                if(dp.x > 0){
                    this.scollToIndex(Math.max(this.currentIdx+1,0));
                } else {
                    this.scollToIndex(Math.min(this.currentIdx-1,len));
                }
            }
            this._touch = null
            return;
        }
        this.hasScroll = true;
        var dx =  event.getLocationX() - this.touchx + this.content.x;
        // cc.log('dx:',dx);
        if(dx > -this.content.width && dx < 0){
            this.content.x = dx;
        }
        this.touchx = event.getLocationX()
    },

    updateIdx(){
        var minx = this.content.width;
        var idx =0;
        for(var i=0; i<this.content.children.length; i++){
            var chd = this.content.children[i];
            var x= chd.x + this.content.x
            if(Math.abs(x) < minx){
                minx = Math.abs(x)
                idx = i;
            }
        }
        this.scollToIndex(idx);
    },

    scollToIndex(idx){
        var child = this.content.children[idx];
        if(!child){
            return;
        }
        
        var dx = this.content.x+child.x
        // if(dx <0 )
        // cc.log('idx:',this.content.x,child.x,dx/200)
        this.content.runAction(cc.moveTo(Math.abs(dx/400),cc.v2(- child.x,this.content.y)));
        if(idx != this.currentIdx){
            this.currentIdx = idx;
            if(this._touch){
                cc.Component.EventHandler.emitEvents(this.changePageEvents, this,idx);
            }
        }
    }

    // update (dt) {},
});
